import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;

/**
 * @Author zhangjinxi
 * @Date: Created in 16:12 2018/1/11
 * @Description:
 */
public class PushExample {

    private static final Logger logger = LoggerFactory.getLogger(PushExample.class);

    public static void main(String[] args) {

        final String MASTER_SECRET = "6e3bc5307b8fe9eec099c0a9";
        final String APP_KEY = "2e0acb4c0623a0b2e1a535bc";

        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

//        PushPayload pushPayload = buildPushObject_all_all_alter();
        PushPayload pushPayload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();


        try {

            PushResult result = jPushClient.sendPush(pushPayload);
            logger.info("Get result : " + result);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }


    }

    public static PushPayload buildPushObject_all_all_alter() {
        final String ALTER = "这里是消息内容！";

        return PushPayload.alertAll(ALTER);
    }

    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        final String MSG_CONTENT = "构建推送对象：平台是 iOS，推送目标是的交集，推送内容同时包括通知与消息 - 通知信息是 ALERT，角标数字为 5，通知声音为，并且附加字段；消息内容是 MSG_CONTENT。通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）";

        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
//                .setAudience(Audience.tag_and(""))
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(MSG_CONTENT))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

}
